/**
 * GLOBAL VARS/OBJECT/CLASS
 */

var Content = new Content();
GLOBAL_Spinner = getSpinner();

// generate spinner
function getSpinner() {
	var spinner = "";
	spinner += " <img";
	spinner += " class='loading'";
	spinner += " style='position:relative; left:48%;'";
	spinner += " src='images/commons/spinner.gif'";
	spinner += " alt='loading ...'";
	spinner += " width='22'";
	spinner += " height='22'";
	spinner += " title='loading ...'";
	spinner += "/>";
	return spinner;
}

/**
 * CONTENT
 */

function Content() {
	_this = this;
	this.errorMsg = "Content, settings error!";

	this.load = function(settings) {
		if (settings == undefined)
			return alert(this.errorMsg);
		if (settings.path == undefined)
			return alert(this.errorMsg);
		if (settings.loadTo == undefined)
			return alert(this.errorMsg);

		$.ajax({
			url : settings.path,
			beforeSend : function() {
				$(settings.loadTo).html("");
			},
			success : function(data) {
				$(settings.loadTo).html(data);
			},
			error : function() {
				var $a = $("a").attr(href, function() {
					this.load(settings);
				});
				$(settings.loadTo)
						.html("Error loading content!\n Retry: " + $a);
			}
		});
	};
}

// error manager
function ErrorManager() {

}

// call function, used for all requests type=json
function Call(settings) {
	var body = null;
	var url = "";
	var params = "";
	var _dataType = "json";
	var responseFunc = null;
	if (settings == undefined)
		return alert("call function, settings error!");
	if (settings.url != undefined)
		url = settings.url;
	if (settings.params != undefined)
		params = settings.params;
	if (settings.dataType != undefined)
		_dataType = settings.dataType;
	if (settings.func != undefined) {
		responseFunc = function(data) {
			settings.responseFunc;
		};
	}
	if (settings.body != undefined)
		body = settings.body;
	$.ajax({
		url : url,
		type : "POST",
		data : params,
		dataType : _dataType,
		beforeSend : function() {
		},
		success : function(data) {
			if (settings.responseFunc == undefined)
				return data;
			// jsonToStr(data);
			settings.responseFunc(data);
		},
		error : function(e) {
			$(body).html(
					"Error!\n Code: " + e.status + "\n Msg: " + e.statusText);
		}
	});
}

/**
 * @object Grid
 * @version demo
 */
function Grid(settings) {
	var _this = this;
	this._head = null;
	this._columns = null;
	this._root = null;
	this._id = null;
	this.init = function(settings) {
		if (settings == undefined)
			return _this.handleError("Grid settings not declared!");
		this._head = settings.head;
		this._columns = new Array();
		if (settings.rootPanel == undefined)
			return _this.handleError("Root panel is undefined!");
		this._root = settings.rootPanel;
		this._id = settings.id;
		this.create();
	};

	this.create = function() {
		var $root = $('<div>', {
			id : _this.id
		});
		$root.addClass('panel');
		$root.append(_this.createHead());
		$(_this._root).append($root);
	};

	this.createHead = function() {
		var $head = $('<div>');
		$head.addClass('headRow');
		for ( var i = 0; i < _this._head.length; i++) {
			var $elem = $('<div>');
			$elem.addClass('col');
			if (_this._head[i].width != undefined)
				$elem.css('width', _this._head[i].width);
			$elem.html(_this._head[i].name);
			$elem.attr('title', _this._head[i].title);
			$elem.appendTo($head);
			_this._columns.push($elem);
		}
		return $head;
	};

	this.addElement = function(settings) {
		var $row = $('<div>', {
			rowData : jsonToStr(settings.rowData)
		});
		$row.addClass('row');
		for ( var i = 0; i < settings.columns.length; i++) {
			var $col = $('<div>');
			$col.addClass('col');
			$col.css({
				"width" : $(_this._columns[i]).width(),
				"height" : settings.height
			});
			$col.html(settings.columns[i].text);
			$row.append($col);
		}
		$row.appendTo(_this._root);
		return $row;
	};

	this.toHTML = function() {

	};
	// handle error
	this.handleError = function(msg) {
		alert(" Error Grid \n Error msg: " + msg);
	};

	this.init(settings);
}

function jsonToStr(json) {
	return JSON.stringify(json);
}

function strToJson(str) {
	eval("var json = " + str);
	return json;
}

function Modal(settings) {
	var _this = this;
	this.modal = null;

	this.getBody = function() {
		return this.modal;
	};

	this.create = function() {
		var $modal = $("<div>", {
			title : settings.title
		}).dialog({
			autoOpen : false,
			height : settings.height,
			minHeight : settings.height,
			width : settings.width,
			minWidth : settings.width,
			modal : true,
			buttons : settings.buttons,
			close : function() {
				try {
					settings.onClose();
				} catch (e) {
				}
				_this.remove();
			}
		});

		return $modal;
	};

	this.init = function(settings) {
		this.modal = this.create();
	};

	this.open = function() {
		$(_this.modal).dialog('open');
	};

	this.remove = function() {
		$(_this.modal).remove();
	};

	this.html = function(htmlContent) {
		$(this.modal).html(htmlContent);
	};

	this.load = function(settings) {
		$.ajax({
			url : settings.url,
			type : "POST",
			data : settings.params,
			beforeSend : function() {
				$(_this.modal).html(
						"<div class='auto-container'>" + getSpinner()
								+ "</div>");
			},
			success : function(data) {
				$(_this.modal).html("");
				$(_this.modal).html(data);
				if (settings.responseFunc != undefined)
					settings.responseFunc();
			},
			error : function(e) {
				$(_this.modal).html(
						"Error!\n Code: " + e.status + "\n Msg: "
								+ e.statusText);
			}
		});
	};

	this.init(settings);
}